package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.data.DataResult;
import com.dongdongshop.data.ResponseStausEnum;
import com.dongdongshop.pojo.TbSpecification;
import com.dongdongshop.pojo.TbSpecificationOption;
import com.dongdongshop.service.SpecificationOptionService;
import com.dongdongshop.service.TbSpecificationService;
import com.dongdongshop.util.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @ClassName SpecificationOptionController
 * @Author RC
 * @date 2020.12.10 08:23
 */
@Controller
@RequestMapping("specification")
public class SpecificationController {
    @Reference(timeout = 100000, check = false)
    private TbSpecificationService tbSpecificationService;
    @Reference(timeout = 100000, check = false)
    private SpecificationOptionService specificationOptionService ;
    private static final Logger logger = LoggerFactory.getLogger(BrandController.class);


    @RequestMapping("getSpecificationList")
    @ResponseBody
    public DataResult getSpecificationList(@RequestParam(defaultValue = "1")Integer pageNumber,@RequestParam(defaultValue = "5")Integer pageSize) {
        try {
            logger.info("进入规格查询方法");
            PageBean<TbSpecification> specificationList = tbSpecificationService.getSpecificationList(pageNumber,pageSize);
            return DataResult.response(ResponseStausEnum.SUSSCESS).setData(specificationList);

        } catch (Exception e) {
            logger.info("查询失败", e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("saveSpciOption")
    @ResponseBody
    public DataResult saveSpciOption(String specName, String options) {
        //保存  数据有空值 数据库保存异常 ,业务方面导致的没有添加成功 falise
        //可以使用map
        try {
            logger.info("进入增加规格方法");
            List<TbSpecificationOption> specificationList = JSONObject.parseArray(options, TbSpecificationOption.class);
            tbSpecificationService.saveSpciOption(specName, specificationList);
            return DataResult.response(ResponseStausEnum.SUSSCESS);

        } catch (Exception e) {
            logger.info("增加失败", e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("toUpdateSpciOption/{id}")
    @ResponseBody
    public DataResult toUpdate(@PathVariable("id")Long id ){
        try {
            logger.info("进入回显方法");
            TbSpecification t = tbSpecificationService.getBrandById(id);
            List<TbSpecificationOption> list = specificationOptionService.getInfoById(id);
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("t",t);
            map.put("list",list);
            return DataResult.response(ResponseStausEnum.SUSSCESS).setData(map);
        }catch (Exception e){
            logger.info("回显失败 ",e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("updateSpciOption")
    @ResponseBody
    public DataResult updateSpciOption(TbSpecification tbSpecification, String OptionString) {
        //保存  数据有空值 数据库保存异常 ,业务方面导致的没有添加成功 falise
        //可以使用map
       try {
            logger.info("进入修改规格方法");
            List<TbSpecificationOption> specificationList = JSONObject.parseArray(OptionString, TbSpecificationOption.class);
            tbSpecificationService.updateSpciOption(tbSpecification, specificationList);
            return DataResult.response(ResponseStausEnum.SUSSCESS);

        } catch (Exception e) {
            logger.info("修改失败", e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("deleteBatchSpecOption/{ids}")
    @ResponseBody
    public DataResult deleteBatchSpecOption(@PathVariable("ids")Integer[] ids){
        try{
            logger.info("进入删除方法");
            tbSpecificationService.deleteBatchSpecOption(ids);
            specificationOptionService.deleteBatchSpecOption(ids);
            return DataResult.response(ResponseStausEnum.SUSSCESS);

        }catch (Exception e){
            logger.info("删除失败",e);
            return DataResult.response(ResponseStausEnum.INTERNAL_SERVER_ERROR);
        }
    }
}
